package com.szh.jexcel.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.szh.jexcel.entity.TemplateEntity;
import com.szh.jexcel.service.ExcelService;

/**
*@author szh
*@date 2022年1月13日
*/
@Controller
public class IndexController {

	@Value("${jexcel.upload.address}")
	private String address;
	@Value("${jexcel.upload.base}")
	private String base;	
	private final Logger LOGGER=LogManager.getLogger();
	@Autowired
	private ExcelService excelService;
	@RequestMapping("/index")
	public ModelAndView index(Model model){
		LOGGER.info("index()...");
		return new ModelAndView("index", "model", model);
	}
	@RequestMapping("/table")
	public ModelAndView table(Model model,Long id){
		model.addAttribute("id", id);
		return new ModelAndView("table", "model", model);
	}
	@RequestMapping("/list")
	@ResponseBody
	public Object list(int page,int limit){
		return excelService.select(page,limit);
	}
	@RequestMapping("/add")
	@ResponseBody
	public Object add(String cells,TemplateEntity excel){
		LOGGER.info("add()...");
		excel.setAddTime(new Date());
		long tempId=excelService.insert(cells, excel);
		JSONObject jo=new JSONObject();
		if(tempId>0){
			jo.put("success", true);
		}
		else{
			jo.put("success", false);
		}
		return jo;
	}
	@RequestMapping("/getDetail")
	@ResponseBody
	public Object getDetail(Long id){
		return excelService.getDetail(id);
	}
	@RequestMapping("/update")
	@ResponseBody
	public Object update(String cells,TemplateEntity excel){
		LOGGER.info("update()...");
		long tempId=excelService.update(cells, excel);
		JSONObject jo=new JSONObject();
		if(tempId>0){
			jo.put("success", true);
		}
		else{
			jo.put("success", false);
		}
		return jo;
	}
	@RequestMapping("/delete")
	@ResponseBody
	public void delete(TemplateEntity excel){
		long tempId=excelService.delete(excel);
		if(tempId>0){
			LOGGER.info("delete()...success");
		}
		else{
			LOGGER.info("delete()...failed");
		}
	}
	@RequestMapping("/upload")
	@ResponseBody
	public JSONObject upload(@RequestParam("file") MultipartFile file,HttpServletRequest request){
		String fileName = file.getOriginalFilename();
		String newName=System.currentTimeMillis()+fileName.substring(fileName.indexOf("."),fileName.length());
		File folder=new File(address);
        if(!folder.exists()) {
        	folder.mkdirs();
        }
		try {
			file.transferTo(new File(folder,newName));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String filePath = base+"/"+newName;
        JSONObject jo=new JSONObject();
        jo.put("path", filePath);
        jo.put("success", true);
        return jo;
	}
}
